<template>
  <el-dialog
    :title="!dataForm.scoreNumber ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="试卷编号" prop="examNumber">
      <el-input v-model="dataForm.examNumber" placeholder="试卷编号"></el-input>
    </el-form-item>
    <el-form-item label="帐号" prop="id">
      <el-input v-model="dataForm.id" placeholder="帐号"></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="idcard">
      <el-input v-model="dataForm.idcard" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
    </el-form-item>
    <el-form-item label="考试时间" prop="testTime">
      <el-input v-model="dataForm.testTime" placeholder="考试时间"></el-input>
    </el-form-item>
    <el-form-item label="有效期" prop="validityTime">
      <el-input v-model="dataForm.validityTime" placeholder="有效期"></el-input>
    </el-form-item>
    <el-form-item label="分数" prop="score">
      <el-input v-model="dataForm.score" placeholder="分数"></el-input>
    </el-form-item>
    <el-form-item label="PDF目录以及文件名称" prop="pdf">
      <el-input v-model="dataForm.pdf" placeholder="PDF目录以及文件名称"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          scoreNumber: 0,
          examNumber: '',
          id: '',
          idcard: '',
          name: '',
          testTime: '',
          validityTime: '',
          score: '',
          pdf: ''
        },
        dataRule: {
          examNumber: [
            { required: true, message: '试卷编号不能为空', trigger: 'blur' }
          ],
          id: [
            { required: true, message: '帐号不能为空', trigger: 'blur' }
          ],
          idcard: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          testTime: [
            { required: true, message: '考试时间不能为空', trigger: 'blur' }
          ],
          validityTime: [
            { required: true, message: '有效期不能为空', trigger: 'blur' }
          ],
          score: [
            { required: true, message: '分数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.scoreNumber = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.scoreNumber) {
            this.$http({
              url: this.$http.adornUrl(`/PCguanli/scorerp/info/${this.dataForm.scoreNumber}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.examNumber = data.scoreRp.examNumber
                this.dataForm.id = data.scoreRp.id
                this.dataForm.idcard = data.scoreRp.idcard
                this.dataForm.name = data.scoreRp.name
                this.dataForm.testTime = data.scoreRp.testTime
                this.dataForm.validityTime = data.scoreRp.validityTime
                this.dataForm.score = data.scoreRp.score
                this.dataForm.pdf = data.scoreRp.pdf
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/PCguanli/scorerp/${!this.dataForm.scoreNumber ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'scoreNumber': this.dataForm.scoreNumber || undefined,
                'examNumber': this.dataForm.examNumber,
                'id': this.dataForm.id,
                'idcard': this.dataForm.idcard,
                'name': this.dataForm.name,
                'testTime': this.dataForm.testTime,
                'validityTime': this.dataForm.validityTime,
                'score': this.dataForm.score,
                'pdf': this.dataForm.pdf
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
